package com.robot.backend.service.impl.user;

import cn.dev33.satoken.secure.BCrypt;
import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.robot.backend.mapper.RecordMapper;
import com.robot.backend.mapper.UserMapper;
import com.robot.backend.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.robot.backend.service.user.userHandleService;
import com.robot.backend.utils.email.registerEmail;

import java.util.HashMap;
import java.util.Map;

@Service
public class userHandleServiceImpl implements userHandleService{
    @Autowired
    UserMapper userMapper;
    @Autowired
    RecordMapper contestMapper;
    @Autowired
    registerEmail registerEmail;

    User regUser = new User();
    String code = new String();
    @Override
    public Map<String,String> registerInfo(String name,String password,String email) {
        Map<String,String> map = new HashMap<>();
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("name", name);
        User user = userMapper.selectOne(queryWrapper);
        if( user !=null) {
            map.put("state","error");
            map.put("info","用户名已存在");
            return map;
        }else{
            map.put("state","success");
        }
        //修改全局变量
        code = registerEmail.sendRegisterEmail(email);
        regUser.setEmail(email);
        regUser.setName(name);
        regUser.setPassword(password);
        return map;
    }

    @Override
    public Map<String, String> registerCode(String name,String password,String email,String codeS) {
        Map<String,String> map = new HashMap<>();
        if(!regUser.getName().equals(name)) return map;
        if(!regUser.getEmail().equals(email)) return map;
        if(!code.equals(codeS)) {
            map.put("state","error");
            map.put("info","验证码错误");
            return map;
        }
        User t = new User();
        t.setId(0);
        t.setName(name);
        t.setPassword(BCrypt.hashpw(password));
        t.setScore(800);
        t.setEmail(email);
        t.setWinn(0);
        t.setContestn(0);
        t.setPhoto("https://img2.baidu.com/it/u=4187311095,1116642930&fm=253&fmt=auto&app=138&f=JPEG?w=380&h=380");
        userMapper.insert(t);
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("name",name);
        StpUtil.login(userMapper.selectOne(queryWrapper).getId());
        map.put("token",StpUtil.getTokenValue());
        map.put("info","success");
        return map;
    }

    @Override
    public Map<String, String> login(String email, String password) {
        Map<String,String> map = new HashMap<>();
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("email",email);
        User user = userMapper.selectOne(queryWrapper);
        if(user == null){
            map.put("state","error");
            map.put("info","密码错误");
            return map;
        }
        if(BCrypt.checkpw(password,user.getPassword())) {
            StpUtil.login(user.getId());
            map.put("token",StpUtil.getTokenValue());
            map.put("name",user.getName());
            map.put("photo",user.getPhoto());
            map.put("state","success");
            map.put("id",user.getId().toString());
        }else{
            map.put("state","error");
            map.put("info","密码错误");
        }
        return map;
    }

    @Override
    public Map<String, String> forgetPassword(String name, String newPassword, String code) {
        return null;
    }
}
